package de.vogella.gwt.helloserver.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Button;

import de.vogella.gwt.helloserver.client.model.Geschaeftspartner;
import com.google.gwt.user.client.ui.CheckBox;

public class GeschaeftspartnerGUI extends Composite{
	
	private VerticalPanel selectionView = new VerticalPanel();
	private FlexTable overview = new FlexTable();
	private FlexTable selectionTable = new FlexTable();
	TextBox tbID = new TextBox();
	TextBox tbBLZ = new TextBox();
	TextBox tbUnternehmen = new TextBox();
	TextBox tbBank = new TextBox();
	TextBox tbVorname = new TextBox();
	TextBox tbUmsatzsteuerID = new TextBox();
	TextBox tbPLZ = new TextBox();
	TextBox tbKontonummer = new TextBox();
	TextBox tbOrt = new TextBox();
	TextBox tbNachname = new TextBox();
	TextBox tbStrasse = new TextBox();
	
	CheckBox chckbxLieferant = new CheckBox("Lieferant");
	CheckBox chckbxKunde = new CheckBox("Kunde");
	TextBox tbHinzuUmsatzsteuer = new TextBox();
	TextBox tbHinzuFax = new TextBox();
	TextBox tbHinzuTelefon = new TextBox();
	TextBox tbHinzuOrt = new TextBox();
	TextBox tbHinzuPLZ = new TextBox();
	TextBox tbHinzuEMail = new TextBox();
	TextBox tbHinzuAdresse = new TextBox();
	TextBox tbHinzuBank = new TextBox();
	TextBox tbHinzuVorname = new TextBox();
	TextBox tbHinzuBLZ = new TextBox();
	TextBox tbHinzuNachname = new TextBox();
	TextBox tbHinzuUnternehmen = new TextBox();
	TextBox tbHinzuID = new TextBox();
	TextBox tbHinzuKontonummer = new TextBox();
	
	Button btnHinzufuegen = new Button("Hinzuf\u00FCgen");
	
	private Geschaeftspartner lastLieferant = new Geschaeftspartner();
	
	private final ServiceAsync service = GWT.create(Service.class);
	

	public GeschaeftspartnerGUI() {
//		RootPanel rootPanel = RootPanel.get();
//		rootPanel.setSize("900", "600");
		
		DecoratedTabPanel tabPanel = new DecoratedTabPanel();
//		rootPanel.add(tabPanel, 10, 0);
		tabPanel.setSize("100px", "100px");		
		
		Grid grid = new Grid(7, 4);
		Grid editView = new Grid(1, 1);
		selectionView.add(grid);
		
		Label labID = new Label("ID");
		grid.setWidget(0, 0, labID);
		
		
		grid.setWidget(0, 1, tbID);
		
		Label labBLZ = new Label("BLZ");
		grid.setWidget(0, 2, labBLZ);
		
		
		grid.setWidget(0, 3, tbBLZ);
		
		Label labUnternehmen = new Label("Unternehmen");
		grid.setWidget(1, 0, labUnternehmen);
		
		
		grid.setWidget(1, 1, tbUnternehmen);
		
		Label labBank = new Label("Bank");
		grid.setWidget(1, 2, labBank);
		
		
		grid.setWidget(1, 3, tbBank);
		
		Label labVorname = new Label("Vorname");
		grid.setWidget(2, 0, labVorname);
		
		
		grid.setWidget(2, 1, tbVorname);
		
		Label lblKontonummer = new Label("Kontonummer");
		grid.setWidget(2, 2, lblKontonummer);
		
		
		grid.setWidget(2, 3, tbKontonummer);
		
		Label labNachname = new Label("Nachname");
		grid.setWidget(3, 0, labNachname);
		
		
		tbNachname.setText("");
		grid.setWidget(3, 1, tbNachname);
		
		Label lblUmsatzsteuerid = new Label("UmsatzsteuerID");
		grid.setWidget(3, 2, lblUmsatzsteuerid);
		
		
		grid.setWidget(3, 3, tbUmsatzsteuerID);
		grid.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_LEFT);
		grid.getCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_BOTTOM);
		grid.getCellFormatter().setVerticalAlignment(3, 3, HasVerticalAlignment.ALIGN_TOP);
		
		Label labPLZ = new Label("PLZ");
		grid.setWidget(4, 0, labPLZ);
		
		
		grid.setWidget(4, 1, tbPLZ);
		
		Label labOrt = new Label("Ort");
		grid.setWidget(4, 2, labOrt);
		
		
		grid.setWidget(4, 3, tbOrt);
		grid.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_LEFT);
		grid.getCellFormatter().setVerticalAlignment(3, 0, HasVerticalAlignment.ALIGN_BOTTOM);
		grid.getCellFormatter().setVerticalAlignment(3, 1, HasVerticalAlignment.ALIGN_BOTTOM);
		
		Label labStrasse = new Label("Stra\u00DFe");
		grid.setWidget(5, 0, labStrasse);
		
		
		tbStrasse.setText("");
		grid.setWidget(5, 1, tbStrasse);
		
		Button btnSuchen = new Button("Suchen");
		grid.setWidget(6, 0, btnSuchen);
		btnSuchen.setWidth("100");
		
		Button btnReset = new Button("Reset");
		grid.setWidget(6, 2, btnReset);
		btnReset.setWidth("100");
		grid.getCellFormatter().setHorizontalAlignment(5, 0, HasHorizontalAlignment.ALIGN_LEFT);
		grid.getCellFormatter().setVerticalAlignment(5, 1, HasVerticalAlignment.ALIGN_BOTTOM);
		
		btnReset.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				tbID.setText("");
				tbUnternehmen.setText("");
				tbOrt.setText("");
				tbVorname.setText("");
				tbNachname.setText("");
				tbBLZ.setText("");
				tbBank.setText("");
				tbKontonummer.setText("");
				tbPLZ.setText("");
				tbUmsatzsteuerID.setText("");				
			}
		});
		
		btnSuchen.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				loadPartner();				
			}
		});
		selectionView.add(selectionTable);
		
		tabPanel.add(overview, "UEbersicht", false);		
		tabPanel.add(selectionView, "Auswahl", false);
		tabPanel.add(editView, "Bearbeiten", false);
		
		initTables();
		loadPartner();
		initWidget(tabPanel);
		
		Grid gHinzu = new Grid(9, 4);
		tabPanel.add(gHinzu, "Hinzufuegen", false);
		gHinzu.setSize("", "");
		
		
		gHinzu.setWidget(0, 0, chckbxLieferant);
		
		
		gHinzu.setWidget(0, 1, chckbxKunde);
		
		Label lblId = new Label("ID");
		gHinzu.setWidget(1, 0, lblId);
		
		
		gHinzu.setWidget(1, 1, tbHinzuID);
		
		Label lblKontonummer_1 = new Label("Kontonummer");
		gHinzu.setWidget(1, 2, lblKontonummer_1);
		
		
		gHinzu.setWidget(1, 3, tbHinzuKontonummer);
		
		Label lblUnternehmen = new Label("Unternehmen");
		gHinzu.setWidget(2, 0, lblUnternehmen);
		
		
		gHinzu.setWidget(2, 1, tbHinzuUnternehmen);
		
		Label lblBlz = new Label("BLZ");
		gHinzu.setWidget(2, 2, lblBlz);
		
		
		tbHinzuBLZ.setText("");
		gHinzu.setWidget(2, 3, tbHinzuBLZ);
		
		Label lblVorname = new Label("Vorname");
		gHinzu.setWidget(3, 0, lblVorname);
		
		
		tbHinzuVorname.setText("");
		gHinzu.setWidget(3, 1, tbHinzuVorname);
		
		Label lblBank = new Label("Bank");
		gHinzu.setWidget(3, 2, lblBank);
		
		
		tbHinzuBank.setText("");
		gHinzu.setWidget(3, 3, tbHinzuBank);
		
		Label lblNachname = new Label("Nachname");
		gHinzu.setWidget(4, 0, lblNachname);
		
		
		gHinzu.setWidget(4, 1, tbHinzuNachname);
		
		Label lblUmsatzsteuerId = new Label("Umsatzsteuer ID");
		lblUmsatzsteuerId.setWordWrap(false);
		gHinzu.setWidget(4, 2, lblUmsatzsteuerId);
		
		
		tbHinzuUmsatzsteuer.setText("");
		gHinzu.setWidget(4, 3, tbHinzuUmsatzsteuer);
		
		Label lblAdresse = new Label("Adresse");
		gHinzu.setWidget(5, 0, lblAdresse);
		
		
		tbHinzuAdresse.setText("");
		gHinzu.setWidget(5, 1, tbHinzuAdresse);
		
		Label lblEmail = new Label("eMail");
		gHinzu.setWidget(5, 2, lblEmail);
		
		
		tbHinzuEMail.setText("");
		gHinzu.setWidget(5, 3, tbHinzuEMail);
		
		Label lblPlz = new Label("PLZ");
		gHinzu.setWidget(6, 0, lblPlz);
		
		
		tbHinzuPLZ.setText("");
		gHinzu.setWidget(6, 1, tbHinzuPLZ);
		
		Label lblTelefon = new Label("Telefon");
		gHinzu.setWidget(6, 2, lblTelefon);
		
		
		tbHinzuTelefon.setText("");
		gHinzu.setWidget(6, 3, tbHinzuTelefon);
		
		Label lblOrt = new Label("Ort");
		gHinzu.setWidget(7, 0, lblOrt);
		
		
		tbHinzuOrt.setText("");
		gHinzu.setWidget(7, 1, tbHinzuOrt);
		
		Label lblFax = new Label("Fax");
		gHinzu.setWidget(7, 2, lblFax);
		
		
		gHinzu.setWidget(7, 3, tbHinzuFax);
		
		Button btnClear = new Button("Clear");
		btnClear.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {	
				tbHinzuUmsatzsteuer.setText("");
				tbHinzuFax.setText("");
				tbHinzuTelefon.setText("");
				tbHinzuOrt.setText("");
				tbHinzuPLZ.setText("");
				tbHinzuEMail.setText("");
				tbHinzuAdresse.setText("");
				tbHinzuBank.setText("");
				tbHinzuVorname.setText("");
				tbHinzuBLZ.setText("");
				tbHinzuNachname.setText("");
				tbHinzuUnternehmen.setText("");
				tbHinzuID.setText("");
				tbHinzuKontonummer.setText("");
			}
		});	
		
		btnHinzufuegen.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {	
				Geschaeftspartner g = new Geschaeftspartner();
				g.setsBank(tbHinzuBank.getText());
				g.setsBLZ(tbHinzuBank.getText());
				g.setsKontonummer(tbHinzuKontonummer.getText());
				g.setsNachname(tbHinzuNachname.getText());
				g.setsOrt(tbHinzuOrt.getText());
				g.setsPLZ(tbHinzuPLZ.getText());
				g.setsStrasse(tbHinzuAdresse.getText());
				g.setsUmsatzsteuerID(tbHinzuUmsatzsteuer.getText());
				g.setsUnternehmen(tbHinzuUnternehmen.getText());
				g.setsVorname(tbHinzuVorname.getText());
				if(chckbxLieferant.getValue()){
					g.setLieferant(true);					
				}
				if(chckbxKunde.getValue()){
					g.setKunde(true);
				}
				service.insertGeschaeftspartner(g, new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {						
						
					}

					@Override
					public void onSuccess(Boolean result) {						
						
					}


				});
			}
		});	

		
		gHinzu.setWidget(8, 0, btnClear);
		
		
		gHinzu.setWidget(8, 2, btnHinzufuegen);
		gHinzu.getCellFormatter().setVerticalAlignment(3, 1, HasVerticalAlignment.ALIGN_BOTTOM);
		gHinzu.getCellFormatter().setVerticalAlignment(4, 1, HasVerticalAlignment.ALIGN_BOTTOM);
		gHinzu.getCellFormatter().setVerticalAlignment(3, 3, HasVerticalAlignment.ALIGN_BOTTOM);
		gHinzu.getCellFormatter().setVerticalAlignment(4, 3, HasVerticalAlignment.ALIGN_TOP);
		

	}

	private void loadPartner(){
		lastLieferant.setSid(tbID.getText());
		lastLieferant.setsBank(tbBank.getText());
		lastLieferant.setsBLZ(tbBLZ.getText());
		lastLieferant.setsKontonummer(tbKontonummer.getText());
		lastLieferant.setsNachname(tbNachname.getText());
		lastLieferant.setsOrt(tbOrt.getText());
		lastLieferant.setsPLZ(tbPLZ.getText());
		lastLieferant.setsStrasse(tbStrasse.getText());
		lastLieferant.setsUmsatzsteuerID(tbUmsatzsteuerID.getText());
		lastLieferant.setsUnternehmen(tbUnternehmen.getText());
		lastLieferant.setsVorname(tbVorname.getText());
		
		service.findLieferant(lastLieferant, new AsyncCallback<List<Geschaeftspartner>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(List<Geschaeftspartner> result) {
				if(result != null && result.size() != 0){
				selectionTable.removeAllRows();
				
				selectionTable.setText(0, 0, "ID");
				selectionTable.setText(0, 1, "Unternehmen");
				selectionTable.setText(0, 2, "Vorname");
				selectionTable.setText(0, 3, "Nachname");
				selectionTable.setText(0, 4, "Strasse");
				selectionTable.setText(0, 5, "PLZ");
				selectionTable.setText(0, 6, "Ort");
				selectionTable.setText(0, 7, "BLZ");
				selectionTable.setText(0, 8, "Bank");
				selectionTable.setText(0, 9, "Kontonummer");
				selectionTable.setText(0, 10, "UmsatzsteuerID");
				
				int i = 1;
				for (Geschaeftspartner l : result) {
					selectionTable.setText(i, 0, l.getSid());
					selectionTable.setText(i, 1, l.getsUnternehmen());
					selectionTable.setText(i, 2, l.getsVorname());
					selectionTable.setText(i, 3, l.getsNachname());
					selectionTable.setText(i, 4, l.getsStrasse());
					selectionTable.setText(i, 5, l.getsPLZ());
					selectionTable.setText(i, 6, l.getsOrt());
					selectionTable.setText(i, 7, l.getsBLZ());
					selectionTable.setText(i, 8, l.getsBank());
					selectionTable.setText(i, 9, l.getsKontonummer());
					selectionTable.setText(i, 10, l.getsUmsatzsteuerID());	
					i++;
				}
				}
				else{
					Window.alert("null or 1");
				}
				
				
			}
		});
	}
	
	private void initTables(){	
		
		service.findLieferant(new Geschaeftspartner(), new AsyncCallback<List<Geschaeftspartner>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(List<Geschaeftspartner> result) {				
				overview.setText(0, 0, "ID");
				overview.setText(0, 1, "Unternehmen");
				overview.setText(0, 2, "Vorname");
				overview.setText(0, 3, "Nachname");
				overview.setText(0, 4, "Strasse");
				overview.setText(0, 5, "PLZ");
				overview.setText(0, 6, "Ort");
				overview.setText(0, 7, "BLZ");
				overview.setText(0, 8, "Bank");
				overview.setText(0, 9, "Kontonummer");
				overview.setText(0, 10, "UmsatzsteuerID");
				
				int i = 1;
				for (Geschaeftspartner l : result) {
					overview.setText(i, 0, l.getSid());
					overview.setText(i, 1, l.getsUnternehmen());
					overview.setText(i, 2, l.getsVorname());
					overview.setText(i, 3, l.getsNachname());
					overview.setText(i, 4, l.getsStrasse());
					overview.setText(i, 5, l.getsPLZ());
					overview.setText(i, 6, l.getsOrt());
					overview.setText(i, 7, l.getsBLZ());
					overview.setText(i, 8, l.getsBank());
					overview.setText(i, 9, l.getsKontonummer());
					overview.setText(i, 10, l.getsUmsatzsteuerID());	
					i++;
				}				
			}
		});
		
		selectionTable.setText(0, 0, "ID");
		selectionTable.setText(0, 1, "Unternehmen");
		selectionTable.setText(0, 2, "Vorname");
		selectionTable.setText(0, 3, "Nachname");
		selectionTable.setText(0, 4, "Strasse");
		selectionTable.setText(0, 5, "PLZ");
		selectionTable.setText(0, 6, "Ort");
		selectionTable.setText(0, 7, "BLZ");
		selectionTable.setText(0, 8, "Bank");
		selectionTable.setText(0, 9, "Kontonummer");
		selectionTable.setText(0, 10, "UmsatzsteuerID");		
	}
}
